#include "myapp.h"
#include "st_dialog.h"
#include <wx/tokenzr.h>
#include <wx/textfile.h>
#include <wx/fontdlg.h>

DECLARE_APP(MyApp);

BEGIN_EVENT_TABLE(SettingDialog, wxDialog)
	EVT_BUTTON(setting_ok,SettingDialog::st_OkBtn_clicked)
	EVT_BUTTON(setting_can,SettingDialog::st_CanBtn_clicked)
	EVT_BUTTON(setting_add,SettingDialog::st_AddPeriodic_clicked)
	EVT_BUTTON(setting_mod,SettingDialog::st_ModPeriodic_clicked)
	EVT_BUTTON(setting_del,SettingDialog::st_DelPeriodic_clicked)
	EVT_BUTTON(setting_baseinfo,SettingDialog::st_InputBaseInfo_clicked)
	EVT_CHOICE(setting_periotiming,SettingDialog::st_PeriodicalTiming_selected)
	EVT_LIST_ITEM_SELECTED(setting_periolist,SettingDialog::st_PeriodicalList_selected)
	EVT_BUTTON(setting_addholiday,SettingDialog::st_AddHoliday_clicked)
	EVT_BUTTON(setting_modholiday,SettingDialog::st_ModHoliday_clicked)
	EVT_BUTTON(setting_delholiday,SettingDialog::st_DelHoliday_clicked)
	EVT_LIST_ITEM_SELECTED(setting_holidaylist,SettingDialog::st_HolidayList_selected)
	EVT_BUTTON(setting_importholiday,SettingDialog::st_ImportHoliday_clicked)
	EVT_LIST_ITEM_SELECTED(setting_budgetlist,SettingDialog::st_BudgetList_selected)
	EVT_BUTTON(setting_addbudget,SettingDialog::st_AddBudget_clicked)
	EVT_BUTTON(setting_modbudget,SettingDialog::st_ModBudget_clicked)
	EVT_BUTTON(setting_delbudget,SettingDialog::st_DelBudget_clicked)
	EVT_CHOICE(setting_budgettype,SettingDialog::st_BudgetType_clicked)
	EVT_CHECKBOX(setting_settlelastday,SettingDialog::st_SettlingLastDay_changed)
	EVT_BUTTON(setting_iconrefbtn,SettingDialog::st_IconRefBtn_clicked)
	EVT_BUTTON(setting_fontrefbtn,SettingDialog::st_FontRefBtn_clicked)
END_EVENT_TABLE() 

//---------------------------------------------------------------
// SettingDialog constructor
//---------------------------------------------------------------
SettingDialog::SettingDialog(wxWindow *parent)
:wxDialog(parent, wxID_ANY, _("Setting of general"), wxDefaultPosition, wxDefaultSize)
{
#ifdef __WXMSW__
	SetClientSize(wxSize(500,375));
#else
	SetClientSize(wxSize(500,380));
#endif

	st_mainPanel = new wxNotebook(this,wxID_ANY,wxPoint(0,0),wxSize(500,340),wxNB_TOP);
	st_mainPanel->AddPage(setup_generalpage(st_mainPanel), _("General"), false);
	//st_mainPanel->AddPage(setup_periodicpage(st_mainPanel), _("Periodic"), false);
	st_mainPanel->AddPage(setup_holidaypage(st_mainPanel), _("Holiday"), false);
	st_mainPanel->AddPage(setup_budgetpage(st_mainPanel), _("Budget"), false);

	st_OkBtn = new wxButton(this, setting_ok, _T("OK"), wxPoint(-1,-1),wxSize(85,-1));
	st_CanBtn = new wxButton(this, setting_can, _("Cancel"), wxPoint(-1,-1),wxSize(85,-1));
	
	de_dialog = new DetailEditor(this);
	wxBoxSizer *bsizer1 = new wxBoxSizer(wxVERTICAL);
		wxBoxSizer *bsizer2 = new wxBoxSizer(wxHORIZONTAL);
		bsizer2->Add(st_OkBtn,0,wxTOP|wxLEFT,0);
		bsizer2->Add(st_CanBtn,0,wxLEFT,5);
	bsizer1->Add(st_mainPanel,0,wxBOTTOM,5);
	bsizer1->Add(bsizer2,0,wxRIGHT|wxALIGN_RIGHT,10);
	this->SetSizer(bsizer1);
	this->Layout();
	listselect = -1;
	listselect_holiday = -1;
	listselect_budget = -1;
	isinputbase = false;
	isinitial = false;
}
//---------------------------------------------------------------
// general page setup function
//---------------------------------------------------------------
wxPanel *SettingDialog::setup_generalpage(wxNotebook *book)
{
	st_generalPanel = new wxPanel(book,wxID_ANY);

	wxString weekday_items[] = {
		_("Sunday"),_("Monday")
	};

	wxStaticText *label1 = new wxStaticText(st_generalPanel,wxID_ANY,_("User name:"));
	st_UserName = new wxTextCtrl(st_generalPanel, wxID_ANY, _T(""),wxDefaultPosition,wxSize(150,-1));

	wxStaticText *label10 = new wxStaticText(st_generalPanel,wxID_ANY,_("Kakeibo-book name:"));
	st_BookName = new wxTextCtrl(st_generalPanel, wxID_ANY, _T(""),wxDefaultPosition,wxSize(150,-1));

	wxStaticText *label2 = new wxStaticText(st_generalPanel,wxID_ANY,_("Beginning to use day:"));
	st_StartDate = new wxDatePickerCtrl(st_generalPanel,wxID_ANY,wxDefaultDateTime,wxDefaultPosition,wxSize(120,-1),wxDP_DEFAULT|wxDP_SHOWCENTURY|wxDP_DROPDOWN);

	wxStaticText *label3 = new wxStaticText(st_generalPanel,wxID_ANY,_("Initial remainder:"));
	st_InitialRemainder = new wxSpinCtrl(st_generalPanel, wxID_ANY, _T("0"),wxDefaultPosition,wxSize(120,-1),wxSP_ARROW_KEYS,0,INT_MAX,0);

	wxStaticText *label4 = new wxStaticText(st_generalPanel,wxID_ANY,_("Settling day:"));
	st_SettlingDay = new wxSpinCtrl(st_generalPanel, wxID_ANY, _T("1"),wxDefaultPosition,wxSize(65,-1),wxSP_ARROW_KEYS,1,31,1);
	st_SettlingLastDay = new wxCheckBox(st_generalPanel,setting_settlelastday, _("Last day of month"));

	wxStaticText *label5 = new wxStaticText(st_generalPanel,wxID_ANY,_("Settling month:"));
	st_SettlingMonth = new wxSpinCtrl(st_generalPanel, wxID_ANY, _T("12"),wxDefaultPosition,wxSize(65,-1),wxSP_ARROW_KEYS,1,12,12);

	wxStaticText *label6 = new wxStaticText(st_generalPanel,wxID_ANY,_("Currency unit:"));
	st_CurrencyUnit = new wxTextCtrl(st_generalPanel, wxID_ANY, _T(""),wxDefaultPosition,wxSize(65,-1));

	wxStaticText *label7 = new wxStaticText(st_generalPanel,wxID_ANY,_("Start day of week:"));
	st_StartWeekDay = new wxChoice(st_generalPanel,wxID_ANY,wxDefaultPosition,wxSize(120,-1),2,weekday_items);
	st_StartWeekDay->SetSelection(0);

	wxStaticText *label8 = new wxStaticText(st_generalPanel,wxID_ANY,_("Icon:"));
	st_IconPath = new wxTextCtrl(st_generalPanel, wxID_ANY, _T(""),wxDefaultPosition,wxSize(230,-1));
	st_IconRefBtn = new wxButton(st_generalPanel, setting_iconrefbtn, _T("..."), wxPoint(-1,-1),wxSize(25,-1));

	wxStaticText *label9 = new wxStaticText(st_generalPanel,wxID_ANY,_("Default font:"));
	st_FontInfo = new wxTextCtrl(st_generalPanel, wxID_ANY, _T(""),wxDefaultPosition,wxSize(230,-1));
	st_FontRefBtn = new wxButton(st_generalPanel, setting_fontrefbtn, _T("..."), wxPoint(-1,-1),wxSize(25,-1));

	st_EncryptFile = new wxCheckBox(st_generalPanel,wxID_ANY, _("Encrypt .kak file"));

	//left column
	wxBoxSizer *bsizer1 = new wxBoxSizer(wxVERTICAL);
	bsizer1->Add(label1,0,wxTOP|wxLEFT,5);
	bsizer1->Add(st_UserName,0,wxLEFT,5);
	bsizer1->Add(label10,0,wxTOP|wxLEFT,5);
	bsizer1->Add(st_BookName,0,wxLEFT,5);
	bsizer1->Add(label2,0,wxTOP|wxLEFT,5);
	bsizer1->Add(st_StartDate,0,wxLEFT,5);
	bsizer1->Add(label3,0,wxTOP|wxLEFT,5);
	bsizer1->Add(st_InitialRemainder,0,wxLEFT,5);
	bsizer1->Add(label4,0,wxTOP|wxLEFT,5);
		wxBoxSizer *bsizer2 = new wxBoxSizer(wxHORIZONTAL);
		bsizer2->Add(st_SettlingDay,0,wxLEFT,0);
		bsizer2->Add(st_SettlingLastDay,0,wxTOP|wxLEFT,3);
	bsizer1->Add(bsizer2,0,wxLEFT,5);
	bsizer1->Add(label5,0,wxTOP|wxLEFT,5);
	bsizer1->Add(st_SettlingMonth,0,wxLEFT,5);
	bsizer1->Add(label6,0,wxTOP|wxLEFT,5);
	bsizer1->Add(st_CurrencyUnit,0,wxLEFT,5);

	//right column
	wxBoxSizer *bsizer3 = new wxBoxSizer(wxVERTICAL);
	bsizer3->Add(label7,0,wxTOP|wxLEFT,5);
	bsizer3->Add(st_StartWeekDay,0,wxLEFT,5);
	bsizer3->Add(label8,0,wxTOP|wxLEFT,5);
		wxBoxSizer *bsizer5 = new wxBoxSizer(wxHORIZONTAL);
		bsizer5->Add(st_IconPath,0,wxLEFT,0);
		bsizer5->Add(st_IconRefBtn,0,wxTOP|wxLEFT,0);
	bsizer3->Add(bsizer5,0,wxLEFT,5);
	bsizer3->Add(label9,0,wxTOP|wxLEFT,5);
		wxBoxSizer *bsizer6 = new wxBoxSizer(wxHORIZONTAL);
		bsizer6->Add(st_FontInfo,0,wxLEFT,0);
		bsizer6->Add(st_FontRefBtn,0,wxTOP|wxLEFT,0);
	bsizer3->Add(bsizer6,0,wxLEFT,5);
	bsizer3->Add(st_EncryptFile,0,wxTOP|wxLEFT,5);

	//concatenate 2-column
	wxBoxSizer *bsizer4 = new wxBoxSizer(wxHORIZONTAL);
	bsizer4->Add(bsizer1,0,wxTOP|wxLEFT,0);
	bsizer4->AddSpacer(50);
	bsizer4->Add(bsizer3,0,wxTOP|wxLEFT,0);
	st_generalPanel->SetSizer(bsizer4);
	st_generalPanel->Layout();
	return st_generalPanel;

}
//---------------------------------------------------------------
// periodical page setup function
//---------------------------------------------------------------
wxPanel *SettingDialog::setup_periodicpage(wxNotebook *book)
{
	st_periodicPanel = new wxPanel(book,wxID_ANY);

	wxString timing_items[] = {
		_("Indicated ordered day of week"),_("Indicated ordered day of month"),
		_("last day of month")
	};
	wxString holiday_items[] = {
		_("no action"),_("work day of before"),_("work day of after")
	};
	st_PeriodicalList = new wxListCtrl(st_periodicPanel, setting_periolist, wxPoint(-1,-1),wxSize(400,150),wxLC_REPORT|wxLC_SINGLE_SEL);
	wxListItem itemcol;
	itemcol.SetText(_("Timing"));
	st_PeriodicalList->InsertColumn(0,itemcol);
	st_PeriodicalList->SetColumnWidth(0, 100);
	itemcol.SetText(_("Handling"));
	st_PeriodicalList->InsertColumn(1,itemcol);
	st_PeriodicalList->SetColumnWidth(1, 100);
	itemcol.SetText(_("Expense"));
	st_PeriodicalList->InsertColumn(2,itemcol);
	st_PeriodicalList->SetColumnWidth(2, 100);
	itemcol.SetText(_("Value"));
	st_PeriodicalList->InsertColumn(3,itemcol);
	st_PeriodicalList->SetColumnWidth(3, 85);
	itemcol.SetText(_("Memo"));
	st_PeriodicalList->InsertColumn(4,itemcol);
	st_PeriodicalList->SetColumnWidth(4, 100);

	st_AddPeriodic = new wxButton(st_periodicPanel, setting_add, _("Add"), wxPoint(-1,-1),wxSize(80,-1));
	st_ModPeriodic = new wxButton(st_periodicPanel, setting_mod, _("Modify"), wxPoint(-1,-1),wxSize(80,-1));
	st_DelPeriodic = new wxButton(st_periodicPanel, setting_del, _("Remove"), wxPoint(-1,-1),wxSize(80,-1));

	wxStaticBox *sbox = new wxStaticBox(st_periodicPanel,wxID_ANY,_("Input base budget"));
	wxStaticBoxSizer *sbsizer = new wxStaticBoxSizer(sbox,wxVERTICAL);
	st_InputBaseInfo = new wxButton(st_periodicPanel, setting_baseinfo, _("Input base"), wxPoint(-1,-1),wxSize(120,-1));
	sbsizer->Add(st_InputBaseInfo,0,wxTOP|wxLEFT);
	st_SetAfterInputValue = new wxCheckBox(st_periodicPanel,wxID_ANY, _("the day will come, open dialog to input value."));
	st_SetAfterInputMemo = new wxCheckBox(st_periodicPanel,wxID_ANY, _("the day will come, open dialog to input memo."));
	st_PeriodicalTiming = new wxChoice(st_periodicPanel,setting_periotiming,wxDefaultPosition,wxSize(100,-1),3,timing_items);
	st_PeriodicalTiming->SetSelection(0);
	wxStaticText *label1 = new wxStaticText(st_periodicPanel,wxID_ANY,_("-th day"));
	st_TimingDay = new wxSpinCtrl(st_periodicPanel, wxID_ANY, _T("1"),wxDefaultPosition,wxSize(65,-1),wxSP_ARROW_KEYS,1,7,1);
	st_CaseHolidayTiming = new wxRadioBox(st_periodicPanel,wxID_ANY,_("Case of holiday"),wxDefaultPosition,wxDefaultSize,3,holiday_items,3,wxRA_SPECIFY_ROWS);

	wxBoxSizer *bsizer1 = new wxBoxSizer(wxVERTICAL);
		wxBoxSizer *bsizer2 = new wxBoxSizer(wxHORIZONTAL);
		bsizer2->Add(st_PeriodicalList,0,wxTOP|wxLEFT,0);
			wxBoxSizer *bsizer3 = new wxBoxSizer(wxVERTICAL);
			bsizer3->Add(st_AddPeriodic,0,wxTOP|wxLEFT,0);
			bsizer3->Add(st_ModPeriodic,0,wxTOP|wxLEFT,0);
			bsizer3->Add(st_DelPeriodic,0,wxTOP|wxLEFT,0);
		bsizer2->Add(bsizer3,0,wxTOP|wxLEFT,0);
	bsizer1->Add(bsizer2,0,wxTOP|wxLEFT,5);
		wxBoxSizer *bsizer4 = new wxBoxSizer(wxHORIZONTAL);
		bsizer4->Add(sbsizer,0,wxTOP|wxLEFT,0);
			wxBoxSizer *bsizer5 = new wxBoxSizer(wxVERTICAL);
			bsizer5->Add(st_SetAfterInputValue,0,wxTOP|wxLEFT,0);
			bsizer5->Add(st_SetAfterInputMemo,0,wxTOP,5);
				wxBoxSizer *bsizer6 = new wxBoxSizer(wxHORIZONTAL);
				bsizer6->Add(st_PeriodicalTiming,0,wxTOP|wxLEFT,0);
				bsizer6->Add(st_TimingDay,0,wxLEFT,5);
				bsizer6->Add(label1,0,wxTOP|wxLEFT,3);
			bsizer5->Add(bsizer6,0,wxTOP,5);
		bsizer4->Add(bsizer5,0,wxLEFT,5);
		bsizer4->Add(st_CaseHolidayTiming,0,wxLEFT,5);
	bsizer1->Add(bsizer4,0,wxTOP|wxLEFT,5);

	st_periodicPanel->SetSizer(bsizer1);
	st_periodicPanel->Layout();
	return st_periodicPanel;

}
//---------------------------------------------------------------
// holiday page setup function
//---------------------------------------------------------------
wxPanel *SettingDialog::setup_holidaypage(wxNotebook *book)
{
	st_holidayPanel = new wxPanel(book,wxID_ANY);

	st_HolidayList = new wxListCtrl(st_holidayPanel, setting_holidaylist, wxPoint(-1,-1),wxSize(250,300),wxLC_REPORT|wxLC_SINGLE_SEL);
	wxListItem itemcol;
	itemcol.SetText(_("Date"));
	st_HolidayList->InsertColumn(0,itemcol);
	st_HolidayList->SetColumnWidth(0, 95);
	itemcol.SetText(_("Holiday name"));
	st_HolidayList->InsertColumn(1,itemcol);
	st_HolidayList->SetColumnWidth(1, 150);

	st_AddHoliday = new wxButton(st_holidayPanel, setting_addholiday, _("Add"), wxPoint(-1,-1),wxSize(80,-1));
	st_ModHoliday = new wxButton(st_holidayPanel, setting_modholiday, _("Modify"), wxPoint(-1,-1),wxSize(80,-1));
	st_DelHoliday = new wxButton(st_holidayPanel, setting_delholiday, _("Remove"), wxPoint(-1,-1),wxSize(80,-1));

	wxStaticBox *sbox = new wxStaticBox(st_holidayPanel,wxID_ANY,_("Setting of holiday"));
	wxStaticBoxSizer *sbsizer = new wxStaticBoxSizer(sbox,wxVERTICAL);

	wxStaticText *label1 = new wxStaticText(st_holidayPanel,wxID_ANY,_("Date as holiday:"));
	st_HolidayDate = new wxDatePickerCtrl(st_holidayPanel,wxID_ANY,wxDefaultDateTime,wxDefaultPosition,wxSize(120,-1),wxDP_DEFAULT|wxDP_SHOWCENTURY|wxDP_DROPDOWN);

	wxStaticText *label2 = new wxStaticText(st_holidayPanel,wxID_ANY,_("Holiday name:"));
	st_HolidayName = new wxTextCtrl(st_holidayPanel, wxID_ANY, _T(""),wxDefaultPosition,wxSize(150,-1));

	wxStaticBox *sboxholiday = new wxStaticBox(st_holidayPanel,wxID_ANY,_("Import holiday data"));
	wxStaticBoxSizer *sbsizer2 = new wxStaticBoxSizer(sboxholiday,wxVERTICAL);
	st_ImportHoliday = new wxButton(st_holidayPanel, setting_importholiday, _("Import from file"), wxPoint(-1,-1),wxSize(150,-1));
	sbsizer2->Add(st_ImportHoliday,0,wxTOP|wxLEFT,0);

	wxBoxSizer *bsizer1 = new wxBoxSizer(wxHORIZONTAL);
	bsizer1->Add(st_HolidayList,0,wxTOP|wxLEFT,5);
		sbsizer->Add(st_AddHoliday,0,wxTOP|wxLEFT,0);
		sbsizer->Add(st_ModHoliday,0,wxTOP|wxLEFT,0);
		sbsizer->Add(st_DelHoliday,0,wxTOP|wxLEFT,0);
		sbsizer->Add(label1,0,wxTOP,10);
		sbsizer->Add(st_HolidayDate,0,wxTOP|wxLEFT,0);
		sbsizer->Add(label2,0,wxTOP,5);
		sbsizer->Add(st_HolidayName,0,wxTOP|wxLEFT,0);
		sbsizer->Add(sbsizer2,0,wxTOP|wxLEFT,0);
	bsizer1->AddSpacer(5);
	bsizer1->Add(sbsizer,0,wxTOP|wxLEFT,5);
	st_holidayPanel->SetSizer(bsizer1);
	st_holidayPanel->Layout();
	return st_holidayPanel;

}
//---------------------------------------------------------------
// budget page setup function
//---------------------------------------------------------------
wxPanel *SettingDialog::setup_budgetpage(wxNotebook *book)
{
	st_budgetPanel = new wxPanel(book,wxID_ANY);

	wxString budget_items[] = {
		_("Monthly"),_("Yearly")
	};
	st_BudgetList = new wxListCtrl(st_budgetPanel, setting_budgetlist, wxPoint(-1,-1),wxSize(400,150),wxLC_REPORT|wxLC_SINGLE_SEL);
	wxListItem itemcol;
	itemcol.SetText(_("type of budget"));
	st_BudgetList->InsertColumn(0,itemcol);
	st_BudgetList->SetColumnWidth(0, 80);
	itemcol.SetText(_("Handling"));
	st_BudgetList->InsertColumn(1,itemcol);
	st_BudgetList->SetColumnWidth(1, 90);
	itemcol.SetText(_("Valid period"));
	st_BudgetList->InsertColumn(2,itemcol);
	st_BudgetList->SetColumnWidth(2, 120);
	itemcol.SetText(_("budget's value"));
	st_BudgetList->InsertColumn(3,itemcol);
	st_BudgetList->SetColumnWidth(3, 100);

	st_AddBudget = new wxButton(st_budgetPanel, setting_addbudget, _("Add"), wxPoint(-1,-1),wxSize(80,-1));
	st_ModBudget = new wxButton(st_budgetPanel, setting_modbudget, _("Modify"), wxPoint(-1,-1),wxSize(80,-1));
	st_DelBudget = new wxButton(st_budgetPanel, setting_delbudget, _("Remove"), wxPoint(-1,-1),wxSize(80,-1));

	wxStaticBox *sbox = new wxStaticBox(st_budgetPanel,wxID_ANY,_("Period of budget"));
	wxStaticBoxSizer *sbsizer = new wxStaticBoxSizer(sbox,wxVERTICAL);
	st_BudgetStart = new wxDatePickerCtrl(st_budgetPanel,wxID_ANY,wxDefaultDateTime,wxDefaultPosition,wxSize(120,-1),wxDP_DEFAULT|wxDP_SHOWCENTURY|wxDP_DROPDOWN);
	wxStaticText *labelto = new wxStaticText(st_budgetPanel,wxID_ANY,_("To"));
	st_BudgetEnd = new wxDatePickerCtrl(st_budgetPanel,wxID_ANY,wxDefaultDateTime,wxDefaultPosition,wxSize(120,-1),wxDP_DEFAULT|wxDP_SHOWCENTURY|wxDP_DROPDOWN);
	sbsizer->Add(st_BudgetStart,0,wxTOP|wxLEFT,0);
	sbsizer->Add(labelto,0,wxTOP|wxLEFT,0);
	sbsizer->Add(st_BudgetEnd,0,wxTOP|wxLEFT,0);
	st_BudgetStart->SetToolTip(_("Ignore the day"));
	st_BudgetEnd->SetToolTip(_("Ignore the day"));

	wxStaticText *label1 = new wxStaticText(st_budgetPanel,wxID_ANY,_("type of budget"));
	st_BudgetType = new wxChoice(st_budgetPanel,setting_budgettype,wxDefaultPosition,wxSize(80,-1),2,budget_items);
	st_BudgetType->SetSelection(0);

	wxStaticText *label2 = new wxStaticText(st_budgetPanel,wxID_ANY,_("budget's value"));
	st_BudgetValue = new wxSpinCtrl(st_budgetPanel, wxID_ANY, _T("0"),wxDefaultPosition,wxSize(95,-1),wxSP_ARROW_KEYS,0,INT_MAX,0);

	wxStaticText *label3 = new wxStaticText(st_budgetPanel,wxID_ANY,_("Handling"));
	st_BudgetHandle = new wxChoice(st_budgetPanel,wxID_ANY,wxDefaultPosition,wxSize(120,-1),0,NULL);

	wxBoxSizer *bsizer1 = new wxBoxSizer(wxVERTICAL);
		wxBoxSizer *bsizer2 = new wxBoxSizer(wxHORIZONTAL);
		bsizer2->Add(st_BudgetList,0,wxTOP|wxLEFT,0);
			wxBoxSizer *bsizer3 = new wxBoxSizer(wxVERTICAL);
			bsizer3->Add(st_AddBudget,0,wxTOP|wxLEFT,0);
			bsizer3->Add(st_ModBudget,0,wxTOP|wxLEFT,0);
			bsizer3->Add(st_DelBudget,0,wxTOP|wxLEFT,0);
		bsizer2->Add(bsizer3,0,wxTOP|wxLEFT,0);
	bsizer1->Add(bsizer2,0,wxTOP|wxLEFT,5);
		wxBoxSizer *bsizer4 = new wxBoxSizer(wxHORIZONTAL);
		bsizer4->Add(sbsizer,0,wxTOP|wxLEFT,0);
			wxBoxSizer *bsizer5 = new wxBoxSizer(wxVERTICAL);
			bsizer5->Add(label1,0,wxTOP|wxLEFT,0);
			bsizer5->Add(st_BudgetType,0,wxTOP|wxLEFT,5);
			bsizer5->Add(label2,0,wxTOP,5);
			bsizer5->Add(st_BudgetValue,0,wxTOP|wxLEFT,5);
		bsizer4->Add(bsizer5,0,wxLEFT,10);
			wxBoxSizer *bsizer6 = new wxBoxSizer(wxVERTICAL);
			bsizer6->Add(label3,0,wxTOP|wxLEFT,0);
			bsizer6->Add(st_BudgetHandle,0,wxTOP|wxLEFT,5);
		bsizer4->Add(bsizer6,0,wxLEFT,10);
	bsizer1->Add(bsizer4,0,wxTOP|wxLEFT,5);
	st_budgetPanel->SetSizer(bsizer1);
	st_budgetPanel->Layout();
	return st_budgetPanel;

}
//---------------------------------------------------------------
// ok button event
//---------------------------------------------------------------
void SettingDialog::st_OkBtn_clicked(wxCommandEvent &event)
{
	this->EndModal(setting_ok);
	this->Hide();
}
//---------------------------------------------------------------
// cancel button event
//---------------------------------------------------------------
void SettingDialog::st_CanBtn_clicked(wxCommandEvent &event)
{
	this->EndModal(setting_can);
	this->Hide();
}
//---------------------------------------------------------------
// st_AddPeriodic button event
//---------------------------------------------------------------
void SettingDialog::st_AddPeriodic_clicked(wxCommandEvent &event)
{
	if (!isinputbase) {
		wxMessageBox(_("you don't to input budget!"),APPTITLE,wxOK|wxICON_ERROR,this);
		return;
	}
	DB_periodical perdb;
	DB_day db;
	//int acc;
	//for DB
	perdb.SetFlag(add_mt);
	perdb.SetDate(wxDateTime::Now());
	perdb.SetInputValue(st_SetAfterInputValue->GetValue());
	perdb.SetInputMemo(st_SetAfterInputMemo->GetValue());
	perdb.SetTiming(st_PeriodicalTiming->GetSelection());
	perdb.SetTimingDay(st_TimingDay->GetValue());
	perdb.SetHoliday(st_CaseHolidayTiming->GetSelection());
	db.SetDate(de_dialog->GetDataDate());
	db.SetId(-1);
	db.SetIO((InoutType)de_dialog->GetDataIncome());
	db.SetExpense(de_dialog->GetDataExpense());
	db.SetItems(de_dialog->GetDataItems());
	db.SetHandling(de_dialog->GetDataHandling());
	if (de_dialog->GetDataIncome() == 0) {
		db.SetIncome(de_dialog->GetDataValue());
		db.SetExpenditure(0);
	}else{
		db.SetIncome(0);
		db.SetExpenditure(de_dialog->GetDataValue());
	}
	db.SetMemo(de_dialog->GetDataMemo());
	perdb.SetBudget(db);
	dbperiod.Add(perdb,1);
	isinputbase = false;
	//for Control
	wxString arr[5];
	if (st_PeriodicalTiming->GetSelection() < 2) {
		arr[0] = st_PeriodicalTiming->GetStringSelection() +
			StrF(_T(": %d %s"),st_TimingDay->GetValue(),_("-th day"));
	}else{
		arr[0] = st_PeriodicalTiming->GetStringSelection();
	}
	arr[1] = de_dialog->GetDataHandlingString();
	arr[2] = de_dialog->GetDataExpenseString() + _T(", ") +
		de_dialog->GetDataItemsString();
	if (db.GetIncome() > 0) {
		arr[3] = IntToStr(db.GetIncome());
	}else if (db.GetExpenditure() > 0) {
		arr[3] = IntToStr(db.GetExpenditure());
	}else{
		arr[3] = _T("0");
	}
	arr[4] = db.GetMemo();
	InsertRow(st_PeriodicalList,arr,5);
}
void SettingDialog::st_ModPeriodic_clicked(wxCommandEvent &event)
{
	if (!isinputbase) {
		wxMessageBox(_("you don't to input budget!"),APPTITLE,wxOK|wxICON_ERROR,this);
		return;
	}
	de_dialog->SetAccount(-1);
	de_dialog->bindData_ExistData(dbperiod.Item(listselect).GetBudget());
	DB_periodical perdb;
	DB_day db;
	//int acc;
	//for DB
	perdb.SetFlag(mod_mt);
	perdb.SetDate(wxDateTime::Now());
	perdb.SetInputValue(st_SetAfterInputValue->GetValue());
	perdb.SetInputMemo(st_SetAfterInputMemo->GetValue());
	perdb.SetTiming(st_PeriodicalTiming->GetSelection());
	perdb.SetTimingDay(st_TimingDay->GetValue());
	perdb.SetHoliday(st_CaseHolidayTiming->GetSelection());
	db.SetDate(de_dialog->GetDataDate());
	db.SetId(-1);
	db.SetIO((InoutType)de_dialog->GetDataIncome());
	db.SetExpense(de_dialog->GetDataExpense());
	db.SetItems(de_dialog->GetDataItems());
	db.SetHandling(de_dialog->GetDataHandling());
	if (de_dialog->GetDataIncome() == 0) {
		db.SetIncome(de_dialog->GetDataValue());
		db.SetExpenditure(0);
	}else{
		db.SetIncome(0);
		db.SetExpenditure(de_dialog->GetDataValue());
	}
	db.SetMemo(de_dialog->GetDataMemo());
	perdb.SetBudget(db);
	dbperiod.Insert(perdb,listselect,1);
	dbperiod.RemoveAt(listselect+1);
	isinputbase = false;
	//for Control
	wxString arr[5];
	if (st_PeriodicalTiming->GetSelection() < 2) {
		arr[0] = st_PeriodicalTiming->GetStringSelection() +
			StrF(_T(": %d %s"),st_TimingDay->GetValue(),_("-th day"));
	}else{
		arr[0] = st_PeriodicalTiming->GetStringSelection();
	}
	arr[1] = de_dialog->GetDataHandlingString();
	arr[2] = de_dialog->GetDataExpenseString() + _T(", ") +
		de_dialog->GetDataItemsString();
	if (db.GetIncome() > 0) {
		arr[3] = IntToStr(db.GetIncome());
	}else if (db.GetExpenditure() > 0) {
		arr[3] = IntToStr(db.GetExpenditure());
	}else{
		arr[3] = _T("0");
	}
	arr[4] = db.GetMemo();
	ChangeRow(st_PeriodicalList,listselect,arr,5);

}
void SettingDialog::st_DelPeriodic_clicked(wxCommandEvent &event)
{
	//for DB
	dbperiod.Item(listselect).SetFlag(del_mt);

	DeleteRow(st_PeriodicalList,listselect);
	de_dialog->ClearControl();
	st_SetAfterInputValue->SetValue(false);
	st_SetAfterInputMemo->SetValue(false);
	st_PeriodicalTiming->SetSelection(0);
	st_TimingDay->SetValue(1);
	st_CaseHolidayTiming->SetSelection(0);
}
void SettingDialog::st_InputBaseInfo_clicked(wxCommandEvent &event)
{
	de_dialog->bindData_InputDate(wxDateTime::UNow());
	if (isinputbase) {
		de_dialog->SetAccount(-1);
		de_dialog->bindData_ExistData(dbperiod.Item(listselect).GetBudget());
	}else{
		de_dialog->SetExpense(in_it);
		de_dialog->SetItems(0);
		de_dialog->SetAccount(-1);
		de_dialog->ChangeEnable(-1,true);
		de_dialog->ClearControl();
	}
	isinputbase = false;
	if (de_dialog->ShowModal() == detaile_ok) {
		isinputbase = true;
	}
}
void SettingDialog::st_PeriodicalTiming_selected(wxCommandEvent &event)
{
	switch (st_PeriodicalTiming->GetSelection()) {
	case 0:
		st_TimingDay->Enable(true);
		st_TimingDay->SetRange(1,7);
		break;
	case 1:
		st_TimingDay->Enable(true);
		st_TimingDay->SetRange(1,31);
		break;
	default:
		st_TimingDay->Enable(false);
		break;
	}
}
void SettingDialog::st_PeriodicalList_selected(wxListEvent &event)
{
	listselect = event.GetIndex();
	if (listselect >= 0) {
		isinputbase = true;
		if (listselect >= (int)dbperiod.size()) {
			return;
		}
		de_dialog->bindData_ExistData(dbperiod.Item(listselect).GetBudget());
		st_SetAfterInputValue->SetValue(dbperiod.Item(listselect).GetInputValue());
		st_SetAfterInputMemo->SetValue(dbperiod.Item(listselect).GetInputMemo());
		//change range of timing value
		wxCommandEvent evt;
		st_PeriodicalTiming_selected(evt);
		st_PeriodicalTiming->SetSelection(dbperiod.Item(listselect).GetTiming());
		st_TimingDay->SetValue(dbperiod.Item(listselect).GetTimingDay());
		st_CaseHolidayTiming->SetSelection(dbperiod.Item(listselect).GetHoliday());
	}else{
		isinputbase = false;
		st_SetAfterInputValue->SetValue(false);
		st_SetAfterInputMemo->SetValue(false);
		st_PeriodicalTiming->SetSelection(0);
		st_TimingDay->SetValue(1);
		st_CaseHolidayTiming->SetSelection(0);
	}
}
void SettingDialog::st_AddHoliday_clicked(wxCommandEvent &event)
{
	wxString arr[2];
	DB_holiday db;
	db.SetDate(st_HolidayDate->GetValue());
	db.SetName(st_HolidayName->GetValue());
	dbholiday.Add(db,1);
	arr[0] = st_HolidayDate->GetValue().Format(_T("%m/%d"));
	arr[1] = st_HolidayName->GetValue();
	InsertRow(st_HolidayList,arr,2);
	st_HolidayName->Clear();
}
void SettingDialog::st_ModHoliday_clicked(wxCommandEvent &event)
{
	if (listselect_holiday < 0) {
		return;
	}
	wxString arr[2];
	DB_holiday db;
	db.SetDate(st_HolidayDate->GetValue());
	db.SetName(st_HolidayName->GetValue());
	dbholiday.Insert(db,listselect_holiday,1);
	dbholiday.RemoveAt(listselect_holiday+1);
	arr[0] = st_HolidayDate->GetValue().Format(_T("%m/%d"));
	arr[1] = st_HolidayName->GetValue();
	ChangeRow(st_HolidayList,listselect_holiday,arr,2);
	st_HolidayName->Clear();

}
void SettingDialog::st_DelHoliday_clicked(wxCommandEvent &event)
{
	if (listselect_holiday < 0) {
		return;
	}
	dbholiday.RemoveAt(listselect_holiday);
	DeleteRow(st_HolidayList,listselect_holiday);
	st_HolidayName->Clear();
}
void SettingDialog::st_HolidayList_selected(wxListEvent &event)
{
	listselect_holiday = event.GetIndex();
	if (listselect_holiday >= 0) {
		st_HolidayDate->SetValue(dbholiday.Item(listselect_holiday).GetDate());
		st_HolidayName->SetValue(dbholiday.Item(listselect_holiday).GetName());
	}else{
		st_HolidayDate->SetValue(wxDateTime::Now());
		st_HolidayName->GetValue().Clear();
	}
}
void SettingDialog::st_BudgetList_selected(wxListEvent &event)
{
	listselect_budget = event.GetIndex();
	if (listselect_budget < 0) {
		return;
	}
	if (dbreserved.size() == 0) {
		return;
	}
	st_BudgetType->SetSelection(dbreserved.Item(listselect_budget).GetType());
	for (int i = 0; i < (int)dbaccountId.size(); i++) {
		if (dbreserved.Item(listselect_budget).GetHandling() == dbaccountId.at(i)) {
			st_BudgetHandle->SetSelection(i);
			break;
		}
	}
	st_BudgetStart->SetValue(dbreserved.Item(listselect_budget).GetStartDate());
	st_BudgetEnd->SetValue(dbreserved.Item(listselect_budget).GetEndDate());
	st_BudgetValue->SetValue(dbreserved.Item(listselect_budget).GetValue());
}
void SettingDialog::st_AddBudget_clicked(wxCommandEvent &event)
{
	DB_reserved db;
	db.SetFlag(add_mt);
	db.SetType(st_BudgetType->GetSelection());
	db.SetHandling(dbaccountId.at(st_BudgetHandle->GetSelection()));
	wxDateTime st, ed;
	CalculateSettleDate(&st, &ed);
	db.SetStartDate(st);
	db.SetEndDate(ed);
	db.SetValue(st_BudgetValue->GetValue());
	if (CheckBudget(db)) {
		wxMessageBox(_("The range of this date repeats.\nCan not add."),APPTITLE,wxOK|wxICON_EXCLAMATION,this);
		return;
	}
	dbreserved.Add(db);
	wxString arr[4];
	arr[0] = st_BudgetType->GetStringSelection();
	arr[1] = st_BudgetHandle->GetStringSelection();
	if (st_BudgetType->GetSelection() == 0) {
		arr[2] = st_BudgetStart->GetValue().Format(_T("%Y/%m")) +
			_("To") + st_BudgetEnd->GetValue().Format(_T("%Y/%m"));
	}else{
		arr[2] = st_BudgetStart->GetValue().Format(_T("%Y")) +
			_("To") + st_BudgetEnd->GetValue().Format(_T("%Y"));
	}
	arr[3] = IntToStr(st_BudgetValue->GetValue());
	InsertRow(st_BudgetList,arr,4);
}
void SettingDialog::st_ModBudget_clicked(wxCommandEvent &event)
{
	if (listselect_budget < 0) {
		return;
	}
	DB_reserved db;
	if (dbreserved.Item(listselect_budget).GetFlag() != add_mt) {
		db.SetFlag(mod_mt);
	}
	db.SetType(st_BudgetType->GetSelection());
	db.SetHandling(dbaccountId.at(st_BudgetHandle->GetSelection()));
	wxDateTime st, ed;
	CalculateSettleDate(&st, &ed);
	db.SetStartDate(st);
	db.SetEndDate(ed);
	db.SetValue(st_BudgetValue->GetValue());
	if (CheckBudget(db)) {
		wxMessageBox(_("The range of this date repeats.\nCan not change."),APPTITLE,wxOK|wxICON_EXCLAMATION,this);
		return;
	}
	dbreserved.Insert(db,listselect_budget,1);
	dbreserved.RemoveAt(listselect_budget+1);
	wxString arr[4];
	arr[0] = st_BudgetType->GetStringSelection();
	arr[1] = st_BudgetHandle->GetStringSelection();
	if (st_BudgetType->GetSelection() == 0) {
		arr[2] = st_BudgetStart->GetValue().Format(_T("%Y/%m")) +
			_("To") + st_BudgetEnd->GetValue().Format(_T("%Y/%m"));
	}else{
		arr[2] = st_BudgetStart->GetValue().Format(_T("%Y")) +
			_("To") + st_BudgetEnd->GetValue().Format(_T("%Y"));
	}
	arr[3] = IntToStr(st_BudgetValue->GetValue());
	ChangeRow(st_BudgetList,listselect_budget,arr,4);

}
void SettingDialog::st_DelBudget_clicked(wxCommandEvent &event)
{
	if (listselect_budget < 0) {
		return;
	}
	if (dbreserved.Item(listselect_budget).GetFlag() == add_mt) {
		dbreserved.RemoveAt(listselect_budget);
	}else{
		dbreserved.Item(listselect_budget).SetFlag(del_mt);
	}
	DeleteRow(st_BudgetList,listselect_budget);

}
void SettingDialog::st_BudgetType_clicked(wxCommandEvent &event)
{

}
void SettingDialog::st_SettlingLastDay_changed(wxCommandEvent &event)
{
	st_SettlingDay->Enable(!(st_SettlingLastDay->GetValue()));
}
void SettingDialog::st_IconRefBtn_clicked(wxCommandEvent &event)
{
	wxFileDialog dialog(this,_("Please choose a file."),_T(""),_T(""),
		_("Image file (*.bmp;*.png;*.ico;*.xpm)|*.bmp;*.png;*.ico;*.xpm;"));
	if (dialog.ShowModal() == wxID_OK) {
		st_IconPath->SetValue(dialog.GetPath());
	}
}
void SettingDialog::st_ImportHoliday_clicked(wxCommandEvent &event)
{
	wxFileDialog dialog(this,_("Please choose a file."),_T(""),_T(""),
		_("CSV file (*.csv)|*.csv;"));
	if (dialog.ShowModal() == wxID_OK) {
		wxTextFile tfile;
		tfile.Open(dialog.GetPath());
		for (wxString str = tfile.GetFirstLine(); !tfile.Eof(); str = tfile.GetNextLine()) {
			DB_holiday db;
			wxStringTokenizer tkz(str,_T(","));
			if (tkz.HasMoreTokens()) {
				wxString token = tkz.GetNextToken();
				wxDateTime date;
				if (date.ParseDate(token.c_str()) == NULL) {
					wxMessageBox(_T("This file is illegal format!"),APPTITLE,wxOK|wxICON_ERROR,this);
					return;
				}else{
					db.SetDate(date);
				}
				db.SetName(tkz.GetNextToken());
			}
			dbholiday.Add(db,1);
			wxString arr[2];
			arr[0] = db.GetDate().Format(_T("%m/%d"));
			arr[1] = db.GetName();
			InsertRow(st_HolidayList,arr,2);
		}
		tfile.Close();
	}

}
void SettingDialog::st_FontRefBtn_clicked(wxCommandEvent &event)
{
	wxFontData fontdata;
	fontdata.SetInitialFont(deffont);
	fontdata.SetChosenFont(deffont);
	wxFontDialog dialog(this,fontdata);
	if (dialog.ShowModal() == wxID_OK) {
		deffont = dialog.GetFontData().GetChosenFont();
		st_FontInfo->SetValue(StrF(_T("%s: %dpt"),deffont.GetFaceName().c_str(),
			deffont.GetPointSize()));
	}
}
//---------------------------------------------------------------
// insert row in wxListCtrl
//---------------------------------------------------------------
void SettingDialog::InsertRow(wxListCtrl *lstcrtl, wxString list[], size_t size)
{
	int index = (lstcrtl->GetItemCount() == 0) ? 0 : lstcrtl->GetItemCount();
	
	long tmp = lstcrtl->InsertItem(index,list[0]);
	wxListItem item;
	item.SetId(tmp);
	for (int i = 1; i < (int)size; i++) {
		item.SetColumn(i);
		item.SetText(list[i]);
		lstcrtl->SetItem(item);
	}
	
}
//---------------------------------------------------------------
// change row in wxListCtrl
//---------------------------------------------------------------
void SettingDialog::ChangeRow(wxListCtrl *lstcrtl, int index, wxString list[], size_t size)
{
	wxListItem item;
	item.SetId(index);
	for (int i = 0; i < (int)size; i++) {
		item.SetColumn(i);
		item.SetText(list[i]);
		lstcrtl->SetItem(item);
	}
}
//---------------------------------------------------------------
// delete row in wxListCtrl
//---------------------------------------------------------------
void SettingDialog::DeleteRow(wxListCtrl *lstcrtl, int index)
{
	lstcrtl->DeleteItem(index);
}
//---------------------------------------------------------------
// bind window data and DB data
//---------------------------------------------------------------
void SettingDialog::BindData_Setting(DB_setting &db)
{
	st_UserName->SetValue(db.GetUserNames());
	st_BookName->SetValue(db.GetBookName());
	st_StartDate->SetValue(db.GetStartDate());
	st_InitialRemainder->SetValue(db.GetRemainder());
	st_SettlingDay->SetValue(db.GetSettlingDay());
	st_SettlingLastDay->SetValue(db.GetSettlingLastDay());
	st_SettlingMonth->SetValue(db.GetSettlingMonth());
	st_CurrencyUnit->SetValue(db.GetCurrencyUnit());
	st_StartWeekDay->SetSelection(db.GetStartWeekDay());
	st_IconPath->SetValue(db.GetIconPath());
	//st_FontInfo->SetValue(StrF(_T("%s: %dpt"),db.GetFontInfo().GetFaceName().c_str(),
	//	db.GetFontInfo().GetPointSize()));
	st_EncryptFile->SetValue(db.GetEncrypt());

	//periodical
	/*dbperiod = db.GetPeriodAll();
	st_PeriodicalList->DeleteAllItems();
	for (int i = 0; i < (int)dbperiod.GetCount(); i++) {
		wxString arr[5];
		st_PeriodicalTiming->SetSelection(dbperiod.Item(i).GetTiming());
		wxCommandEvent evt;
		st_PeriodicalTiming_selected(evt);
		st_TimingDay->SetValue(dbperiod.Item(i).GetTimingDay());
		if (st_PeriodicalTiming->GetSelection() < 2) {
			arr[0] = st_PeriodicalTiming->GetStringSelection() +
				StrF(_T(": %d %s"),st_TimingDay->GetValue(),_("-th day"));
		}else{
			arr[0] = st_PeriodicalTiming->GetStringSelection();
		}
		arr[1] = wxGetApp().AppDB.GetAccount(dbperiod.Item(i).GetBudget().GetHandling()).GetName();
		wxString sql = StrF(_T("select ex_name, it_name from db_expense, db_items where ex_id = %d and it_id = %d and ex_id = it_expense;"),
			dbperiod.Item(i).GetBudget().GetExpense(),dbperiod.Item(i).GetBudget().GetItems());
		wxSQLite3ResultSet rset = wxGetApp().SQLDB().ExecuteQuery(sql);
		while (rset.NextRow()) {
			arr[2] = rset.GetString(0) + _T(", ") +
				rset.GetString(1);
		}
		if (dbperiod.Item(i).GetBudget().GetIncome() > 0) {
			arr[3] = IntToStr(dbperiod.Item(i).GetBudget().GetIncome());
		}else if (dbperiod.Item(i).GetBudget().GetExpenditure() > 0) {
			arr[3] = IntToStr(dbperiod.Item(i).GetBudget().GetExpenditure());
		}else{
			arr[3] = _T("0");
		}
		arr[4] = dbperiod.Item(i).GetBudget().GetMemo();
		InsertRow(st_PeriodicalList,arr,5);
	}
	wxCommandEvent evt;
	st_PeriodicalTiming_selected(evt);
	st_SettlingLastDay_changed(evt);*/
	//holiday
	//dbholiday.assign(db.GetHolidayAll().begin(),db.GetHolidayAll().end());
	dbholiday = db.GetHolidayAll();
	//std::vector <DB_holiday>::iterator hit;
	st_HolidayList->DeleteAllItems();
	for (int i = 0; i < (int)dbholiday.GetCount(); i++) {
	//for (hit = dbholiday.begin(); hit != dbholiday.end(); hit++) {
		wxString arr[2];
		arr[0] = dbholiday.Item(i).GetDate().Format(_T("%m/%d"));
		arr[1] = dbholiday.Item(i).GetName();
		InsertRow(st_HolidayList,arr,2);
	}
	//reserved budget
	dbreserved = db.GetBudgetAll();
	st_BudgetList->DeleteAllItems();
	for (int i = 0; i < (int)dbreserved.GetCount(); i++) {
		wxString arr[4];
		arr[0] = st_BudgetType->GetString(dbreserved.Item(i).GetType());
		for (int j = 0; j < (int)dbaccountId.size(); j++) {
			if (dbreserved.Item(i).GetHandling() == dbaccountId.at(j)) {
				arr[1] = st_BudgetHandle->GetString(j);
				break;
			}
		}
		if (dbreserved.Item(i).GetType() == 0) {
			arr[2] = dbreserved.Item(i).GetStartDate().Format(_T("%Y/%m")) +
				_("To") + dbreserved.Item(i).GetEndDate().Format(_T("%Y/%m"));
		}else{
			arr[2] = dbreserved.Item(i).GetStartDate().Format(_T("%Y")) +
				_("To") + dbreserved.Item(i).GetEndDate().Format(_T("%Y"));
		}
		arr[3] = IntToStr(dbreserved.Item(i).GetValue());
		InsertRow(st_BudgetList,arr,4);
	}
}
//---------------------------------------------------------------
// get window data as DB data
//---------------------------------------------------------------
DB_setting &SettingDialog::GetData_Setting(void)
{
	//DB_setting setdb;
	setdb.SetUserName(st_UserName->GetValue());
	setdb.SetBookName(st_BookName->GetValue());
	setdb.SetStartDate(st_StartDate->GetValue());
	setdb.SetRemainder(st_InitialRemainder->GetValue());
	setdb.SetSettlingDay(st_SettlingDay->GetValue());
	setdb.SetSettlingLastDay(st_SettlingLastDay->GetValue());
	setdb.SetSettlingMonth(st_SettlingMonth->GetValue());
	setdb.SetCurrencyUnit(st_CurrencyUnit->GetValue());
	setdb.SetStartWeekDay(st_StartWeekDay->GetSelection());
	setdb.SetIconPath(st_IconPath->GetValue());
	setdb.SetEncrypt(st_EncryptFile->GetValue());
	//setdb.SetFontInfo(deffont);
	//periodical
	setdb.ClearPeriod();
	for (int i = 0; i < (int)dbperiod.GetCount(); i++) {
		setdb.AddPeriod(dbperiod.Item(i));
	}
	//holiday
	setdb.ClearHoliday();
	for (int i = 0; i < (int)dbholiday.GetCount(); i++) {
		setdb.AddHoliday(dbholiday.Item(i));
	}
	//reserved budget
	setdb.ClearBudget();
	for (int i = 0; i < (int)dbreserved.GetCount(); i++) {
		setdb.AddBudget(dbreserved.Item(i));
	}
	return setdb;
}
//---------------------------------------------------------------
// set mode as initial data
//---------------------------------------------------------------
void SettingDialog::SetInitialMode(bool flag)
{
	st_mainPanel->SetSelection(0);
	//st_periodicPanel->Enable(!flag);
	st_holidayPanel->Enable(!flag);
	st_budgetPanel->Enable(!flag);
	//st_CanBtn->Enable(!flag);
	isinitial = flag;
	st_InitialRemainder->Enable(flag);
}
//---------------------------------------------------------------
// calculate settle date
//---------------------------------------------------------------
void SettingDialog::CalculateSettleDate(wxDateTime *stdate, wxDateTime *eddate)
{
	wxDateTime ctrl_stdate, ctrl_eddate;
	ctrl_stdate = st_BudgetStart->GetValue();
	ctrl_eddate = st_BudgetEnd->GetValue();
	int cst_year = ctrl_stdate.GetYear();
	int ced_year = ctrl_eddate.GetYear();
	//get month start date and end date
	wxDateTime::Month cst_month = ctrl_stdate.GetMonth();
	wxDateTime::Month ced_month = ctrl_eddate.GetMonth();
	//if budget type is year
	if (st_BudgetType->GetSelection() == 1) {
		ctrl_eddate.SetMonth((wxDateTime::Month)(st_SettlingMonth->GetValue()-1));
		cst_month = ctrl_stdate.GetMonth();
		wxDateTime tmp = ctrl_eddate;
		tmp.Subtract(wxDateSpan(1,0,0,0));
		tmp.Add(wxDateSpan(0,1,0,0));
		ctrl_stdate.SetMonth(tmp.GetMonth());
		ctrl_stdate.SetYear(tmp.GetYear());
	}
	//if settling day is last day of month
	if (st_SettlingLastDay->GetValue()) {
		ctrl_eddate.SetToLastMonthDay(ced_month,ced_year);
		ctrl_stdate.SetDay(1);
	}else{
		//settling day is **not** last day of month
		wxDateTime lasteddate = ctrl_eddate.GetLastMonthDay(ced_month,ced_year);
		if (st_SettlingDay->GetValue() > lasteddate.GetDay()) {
			ctrl_eddate.SetToLastMonthDay(ced_month,ced_year);
			ctrl_stdate.SetDay(1);
		}else{
			ctrl_eddate.SetDay(st_SettlingDay->GetValue());
			wxDateTime tmp = ctrl_eddate;
			tmp.Add(wxDateSpan(0,0,0,1));
			ctrl_stdate.SetDay(tmp.GetDay());
			if (ctrl_stdate.GetMonth() >= ctrl_eddate.GetMonth()) {
				ctrl_stdate.Subtract(wxDateSpan(0,1,0,0));
			}
		}
	}
	*stdate = ctrl_stdate;
	*eddate = ctrl_eddate;
}
//---------------------------------------------------------------
// setup account data in combobox 
//---------------------------------------------------------------
void SettingDialog::SetAccount(void)
{
	wxString sql = _T("select * from db_account");
	wxSQLite3ResultSet rset = wxGetApp().SQLDB().ExecuteQuery(sql);
	st_BudgetHandle->Clear();
	dbaccountId.clear();
	while (rset.NextRow()) {
		dbaccountId.push_back(rset.GetInt(0));
		st_BudgetHandle->Append(rset.GetString(1));
	}
	st_BudgetHandle->SetSelection(0);
}
//---------------------------------------------------------------
// check weather paramater date is between start date and end date in DB 
//---------------------------------------------------------------
bool SettingDialog::CheckBudget(DB_reserved &db)
{
	for (int i = 0; i < (int)dbreserved.GetCount(); i++) {
		DB_reserved *tmp = &(dbreserved.Item(i));
		if (tmp->GetFlag() != del_mt) {
			if (db.GetStartDate().IsBetween(tmp->GetStartDate(),tmp->GetEndDate())) {
				return true;
			}
			if (db.GetEndDate().IsBetween(tmp->GetStartDate(),tmp->GetEndDate())) {
				return true;
			}
			if (tmp->GetStartDate().IsBetween(db.GetStartDate(),db.GetEndDate())) {
				return true;
			}
			if (tmp->GetEndDate().IsBetween(db.GetStartDate(),db.GetEndDate())) {
				return true;
			}
		}
	}
	return false;
}